package com.waterworld.watch.chat.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.chat.activity.ChatActivity;
import com.waterworld.watch.chat.event.NotReadMsgEvent;
import com.waterworld.watch.common.application.MyApplication;
import com.waterworld.watch.common.bean.ChatGroupBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.db.DbController;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.push.contant.MessageContants;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by nhuan
 * Time:2019/3/14.
 */

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MyHolder>{

    private Context mContext;
    private List<ChatGroupBean> mList;
    private DbController dbController = DbController.getInstance(MyApplication.getContext());
    public ChatAdapter (Context context,List<ChatGroupBean> list) {
        mContext = context;
        mList = list;
    }
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_chat_group,viewGroup,false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ChatAdapter.MyHolder myHolder, int i) {
        if (mList.get(i).getHead() != null) {
            Glide.with(mContext).load(UrlContants.FILE_URL + "resources/watch/" + mList.get(i).getHead()).into(myHolder.iv_group_avatar);
        } else {
            Glide.with(mContext).load(R.drawable.sys_parent_01).into(myHolder.iv_group_avatar);
        }
        myHolder.tv_group_name.setText(mList.get(i).getName());
        if (mList.get(i).getMaxMsgId() != null) {
            //如果有新消息或者未读消息
            if (dbController.getLatestMsg(mList.get(i).getGroupId()) == null || dbController.getLatestMsg(mList.get(i).getGroupId()) != null && mList.get(i).getMaxMsgId() > dbController.getLatestMsg(mList.get(i).getGroupId()).getMsgId() || dbController.hasUnreadMsg(mList.get(i).getGroupId())) {
                myHolder.iv_red_dot.setVisibility(View.VISIBLE);
                //这里的消息是指已经看到了但是未读
                EventBus.getDefault().post(new NotReadMsgEvent(MessageContants.CHAT, mList.get(i).getGroupId()));
            }
        }
        myHolder.rl_chat_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.startActivity(new Intent(mContext, ChatActivity.class).putExtra("chatGroup",mList.get(i)));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rl_chat_group)
        RelativeLayout rl_chat_group;

        @BindView(R.id.iv_group_avatar)
        CircleImageView iv_group_avatar;

        @BindView(R.id.tv_group_name)
        TextView tv_group_name;

        @BindView(R.id.tv_time)
        TextView tv_time;

        @BindView(R.id.tv_sound)
        TextView tv_sound;

        @BindView(R.id.iv_red_dot)
        ImageView iv_red_dot;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
